<?php

class Admin_AdsforController extends Zend_Controller_Action
{
	private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
        $modelsA=new Admin_Model_Adsfor();
        $modelsP=new Admin_Model_Project();
        $data=$modelsA->getAllData();
        $dataMerge=array();
        foreach($data->toArray() as $key=>$val){
            if(isset($val['project']) && $val['project']){
                $val['projectName']=$modelsP->getDataById($val['project']);
            }
            $dataMerge[]=$val;
        }
        $this->view->forList=$dataMerge;        
    }
    public function detailAction(){
        $modelsA=new Admin_Model_Adsfor();
        $modelsP=new Admin_Model_Project();
        $modelsO=new Admin_Model_Other();
        $adsId=$this->_getParam('id');
        $for=$modelsA->getDataById($adsId)->toArray();
         
        $arrOther=json_decode($for['other'],true);
        
        $dataOther=array();
        $dataOther1=array();
        foreach($arrOther as $k=>$value){
            $otherName=$modelsO->getDataById($k);
            $dataOther[$otherName['name']]=$value;
        }
        
        $this->view->other=$dataOther;
        if(isset($for['projectId']) && $for['projectId']){
            $for['projectName']=$modelsP->getDataById($for['projectName']);
        }
        $this->view->adsFor=$for;
        $conUnit=new Zend_Config_Ini(CONFIG_PATH.'/unit.ini',APPLICATION_ENV);
        $this->view->configUnit=$conUnit;
    }
    public function addAction(){
        $form=new Admin_Form_Adsfor();
        $this->view->form = $form;
        $modelsA=new Admin_Model_Adsfor();
        $modelsPr=new Admin_Model_Project();
        $modelsO=new Admin_Model_Other();
        $modelsE=new Admin_Model_Estate();
        $modelsP=new Admin_Model_Province();
        $province= $modelsP->getAllData()->toArray();
        $this->view->project=$modelsPr->getAllData();
        $configCar= new Zend_Config_Ini(CONFIG_PATH.'/car.ini', APPLICATION_ENV);
        $this->view->car=$configCar->car->unit->toArray();
        $this->view->province=$province;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();            
            if($formData){
                    
                $other = array();
                foreach($formData as $key=>$value){
                    if(is_numeric($key) && $value){
                        $other[$key]=$value;
                    }
                    elseif($key=='otherUtility'){
                        foreach($value as $row){
                            $other[$row]=1;
                        }
                    }
                }
                
                $shortTitle= $this->_helper->setup($formData['title']);                
                if($formData['title'] && $formData['contact'] && $formData['mobile'] && $formData['transaction']){
                    $modelAdsFor = new Admin_Model_Adsfor();
                    $adsId = $modelAdsFor->addData($formData['title'],$shortTitle,time($formData['createDate']),time($formData['endDate'])
                    ,time($formData['startDate']),$formData['content'],$formData['transaction'],$formData['estate'],$formData['area'],$formData['price']
                    ,$formData['unit'],$formData['total'],$formData['address'],$formData['province'],$formData['district'],$formData['ward']
                    ,$formData['project'],$formData['contact'],$formData['mobile'],$formData['phoneContact'],$formData['email'],1,json_encode($other));
                    if($adsId){
                        $this->_helper->redirector('index', 'adsfor', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                    $this->view->flag = "error";
                }
            }else{
                $this->_helper->redirector('index', 'adsfor', 'admin');
            }
        }//end ispost
        $other=$modelsO->getAllData()->toArray();    
        $this->view->other=$other;
        $arrayInfo=array();
        foreach($other as $value){
            if($value['groupInfo']=='otherInfo' && $value['elementType']!='select'){
                $arrayInfo[]=$value;
            }
        }
        $this->view->otherInfo=$arrayInfo;
        //$this->view->estate= $modelsE->getAllData()->toArray();               
    }
    public function deleteAction(){
       
    }
    public function editAction(){
        
        
    }
}





